package com.qingshui.browser.ui.views;

import com.qingshui.browser.R;


import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.LinearLayout;

public class HighLightLinearLayout extends LinearLayout {
	private int offset;
	private int frontIndex;
	public HighLightLinearLayout(Context context) {
		super(context);
		frontIndex=-1;
		offset=-50;
		init();
		// TODO Auto-generated constructor stub
	}
	public HighLightLinearLayout(Context context , AttributeSet attr){
		super(context, attr);
		frontIndex =-1;
		TypedArray a = context.obtainStyledAttributes(attr,R.styleable.HighLightLinearLayout);
		offset=a.getInteger(R.styleable.HighLightLinearLayout_offset, -50);
		
		init();
	}
	
	protected void init(){
		this.setChildrenDrawingOrderEnabled(true);
//		try{
//			Field f1=Class.forName("ViewGroup").getDeclaredField("mGroupFlags");
//			f1.setAccessible(true);
//			Field f2=super.getClass().getDeclaredField("FLAG_USE_CHILD_DRAWING_ORDER");
//			int mGroupFlog=f1.getInt(this);
//			int FLAG_ORDER=f2.getInt(this);
//			f1.set(this, mGroupFlog|FLAG_ORDER);
//			
//			
//		}catch(Exception e){
//			e.printStackTrace();
//		}
//		
	}
	
	public void bringChildToFront(View view){
		frontIndex=indexOfChild(view);
	}
	
	public int getForntView(){
		return frontIndex;
	}
	protected int getChildDrawingOrder(int childCount, int i) {
		
		int index =this.frontIndex;
		if(index<0||index>=childCount){
			index=i;
			}
		else{
			if(childCount-1!=i){//��Ϊ���һ��
				int lastIndex=childCount-1-i;
				if(lastIndex>index){//�ȵ�ǰ��  ����Ƚ����
					index=lastIndex ;
				}else{
					index= lastIndex-1;
				}
			}
			
		}
		//Log.d("Log", i+":"+index);
		return index;
	}
	
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        
    }
	@Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
       super.onLayout(changed, l, t, r, b);
       
    }
	int getLocationOffset(View child) {
		return 0;
        //return -50*indexOfChild(child);
    }
	int getNextLocationOffset(View child) {
		if(indexOfChild(child)==getChildCount()-1)return 0;
		return offset;
		//return -50*indexOfChild(child);
		
    }
	public int getOffset() {
		return offset;
	}
	public void setOffset(int offset) {
		this.offset = offset;
	}
	
	public boolean onTouchEvent(MotionEvent event){
		//Log.d("tgg", "ttt");
		return super.onTouchEvent(event);
	}
}
